package com.kcst.configure;

import android.os.Environment;
import android.util.Log;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;

/**
 * @author: glc.
 * Date: 2022/8/10  19:25
 * Description:
 */
public class FileUtils {

    private static final String FILE_NAME = "/KingKong/app/";

    public static String read() {
        try {
            // 如果手机插入了SD卡，而且应用程序具有访问SD的权限
            if (Environment.getExternalStorageState().equals(
                    Environment.MEDIA_MOUNTED)) {
                // 获取SD卡对应的存储目录
                File sdCardDir = Environment.getExternalStorageDirectory();
                String targeFile = sdCardDir.getPath() + FILE_NAME + "Configure.txt";
                if (!(new File(targeFile)).exists()){return "";}
                // 获取指定文件对应的输入流
                FileInputStream fis = new FileInputStream(targeFile);
                // 将指定输入流包装成BufferedReader
                BufferedReader br = new BufferedReader(new
                        InputStreamReader(fis));
                StringBuilder sb = new StringBuilder("");
                String line = null;
                // 循环读取文件内容
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
                // 关闭资源
                br.close();
                return sb.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void write(String content) {
        try {
            // 如果手机插入了SD卡，而且应用程序具有访问SD的权限
            if (Environment.getExternalStorageState().equals(
                    Environment.MEDIA_MOUNTED)) {
                // 获取SD卡的目录
                File sdCardDir = Environment.getExternalStorageDirectory();
                File targetFile = new File(sdCardDir.getPath() + FILE_NAME + "Configure.txt");
                if (targetFile.exists()){
                    targetFile.delete();
                }
                File iniDir = new File(sdCardDir.getPath() + FILE_NAME );
                if (!iniDir.exists()){
                    iniDir.mkdirs();
                }
                File file = new File(iniDir.getPath() + "/Configure.txt");
                if (!file.exists()){
                    file.createNewFile();
                }

                // 以指定文件创建 RandomAccessFile对象
                RandomAccessFile raf = new RandomAccessFile(
                        file, "rw");
                // 将文件记录指针移动到最后
                raf.seek(file.length());
                // 输出文件内容
                raf.write(content.getBytes());
                // 关闭RandomAccessFile
                raf.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void writeKey(String key,String filePath){
        try{
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
                File sdCardDir = Environment.getExternalStorageDirectory();
                File targetFile = new File(sdCardDir.getCanonicalPath() + filePath);
                if (targetFile.exists()) {return;}
                RandomAccessFile raf = new RandomAccessFile(
                        targetFile, "rw");
                // 将文件记录指针移动到最后
                raf.seek(targetFile.length());
                // 输出文件内容
                raf.write(key.getBytes());
                // 关闭RandomAccessFile
                raf.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static String readKey(String filePath){
        try {
            // 如果手机插入了SD卡，而且应用程序具有访问SD的权限
            if (Environment.getExternalStorageState().equals(
                    Environment.MEDIA_MOUNTED)) {
                // 获取SD卡对应的存储目录
                File sdCardDir = Environment.getExternalStorageDirectory();
                // 获取指定文件对应的输入流
                FileInputStream fis = new FileInputStream(
                        sdCardDir.getCanonicalPath() + filePath);
                // 将指定输入流包装成BufferedReader
                BufferedReader br = new BufferedReader(new
                        InputStreamReader(fis));
                StringBuilder sb = new StringBuilder("");
                String line = null;
                // 循环读取文件内容
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
                // 关闭资源
                br.close();
                return sb.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
